// pages/orders/order.js
var app = getApp();
var orderId = 0;
Page({
  data:{
    showRefundApply: false
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    orderId = options.id;

  },
  onReady:function(){
    // 页面渲染完成
    this.getOrders();
  },
  getOrders: function(){
    var that = this;
    wx.showLoading({ title: '订单加载中...', mask: true });
    app.func.req('orders/show', { id: orderId }, 'GET', function (res) {
      console.log(res.result)
      var order = res.result;
      order.status_alias = app.util.orderStatus(order.status)
      that.setData({
        order: order
      });
      //获取店铺信息
      app.func.req('shops/show', {
        id: res.result.shop_id
      }, 'GET', function (res) {
        that.setData({
          shop: res.result
        });
      }, {
          'token': wx.getStorageSync('token')
        });
      wx.hideLoading();
    }, {
        token: wx.getStorageSync('token')
      })
  },
  pay: function () {
    var that = this;
    app.func.req('pay/xcx', {
      tran_no: that.data.order['tran_no']
    }, 'POST', function (res) {
        console.log(res);
          if(res.msg == 'status'){
            wx.showToast({
              title: '商品已下架',
              image: '/images/fail.png',
              duration: 2000
            });          
            return;
          }
          if(res.msg == 'product.num.error'){
            wx.showToast({
              title: '库存不足',
              image: '/images/fail.png',
              duration: 2000
            });
            return;
          }
      console.log(res);
      var prepay_id = res.result['prepay_id'];
      console.log('pay=>'+JSON.stringify(res.result));
      wx.requestPayment({
        'timeStamp': res.result['timestamp'] + '',
        'nonceStr': res.result['nonce_str'],
        'package': 'prepay_id=' + prepay_id,
        'signType': 'MD5',
        'paySign': res.result['pay_sign'],
        'success': function (res) {
          app.func.req('orders/prepay_id', {
            tran_no: that.data.order['tran_no'],
            prepay_id: prepay_id
          }, 'POST', function(res){
            console.log(res);
          },{
            'token': wx.getStorageSync('token')
          });
          that.getOrders();
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 2000
          })
        },
        'fail': function (res) {
          wx.showToast({
            title: '支付失败',
            image: '/images/fail.png',
            duration: 2000
          });
        }
      });
    }, {
      'token': wx.getStorageSync('token')
    })
  },
  contact: function(){
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.shop.kfphone ? that.data.shop.kfphone : that.data.shop.phone
    })
  },
  receive: function(){
    var that = this;
    app.func.req('orders/receive', { id: orderId }, 'POST', function (res) {
      if (res.code = 'success') {
        wx.showToast({
          title: '收货成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateBack()
        }, 2000)
      }
    }, {
        token: wx.getStorageSync('token')
      })
  },
  refundApply: function (e) {
    var that = this;
    var reason = e.detail.value.reason;
    if (reason.length < 5) {
      wx.showModal({
        title: '提示',
        content: '申请理由不少于5个字',
        showCancel: false
      });
      return;
    }
    this.setData({
      showRefundApply: false
    })
    app.func.req('orders/refund', {
       id: orderId,
       reason: reason,
    }, 'POST', function (res) {
      if (res.code == 'success') {
        wx.showModal({
          title: '提示',
          content: '已提交，等待处理',
          showCancel: false,
          success: function(){
            setTimeout(function () {
              wx.navigateBack()
            }, 1000)
          }
        });
      } else {
        wx.showToast({
          title: '提交失败',
          image: '/images/fail.png',
          duration: 2000
        });
      }
    }, {
        token: wx.getStorageSync('token')
      })
  },
  showRefundApply: function () {
    this.setData({
      showRefundApply: true
    })
  },
  closeRefundApply: function () {
    this.setData({
      showRefundApply: false
    })
  },
  copyinfo: function (e) {
    var that = this;
    wx.setClipboardData({
      data: e.target.dataset.info,
      success: function (res) {
        wx.showToast({
          title: '复制成功！',
          icon: 'success',
          duration: 2000,
          mask: true
        })
      }
    });
  }
})
